package com.code.coinprice.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 响应工具类
 */
@Component
public class ResponseUtil {

    /**
     * 业务异常
     */
    public static final int BUSINESS_ERR_NO = 600;

    public static final String ERR_NO = "code";
    public static final String ERR_MSG = "msg";
    public static final String DATA = "data";

    public static Object ok() {
        Map<String, Object> obj = new HashMap<>(2);
        obj.put(ERR_NO, 0);
        obj.put(ERR_MSG, messageSource.getMessage("0", null, LocaleContextHolder.getLocale()));
        return obj;
    }

    public static Object ok(Object data) {
        Map<String, Object> obj = new HashMap<>(3);
        obj.put(ERR_NO, 0);
        obj.put(ERR_MSG, messageSource.getMessage("0", null, LocaleContextHolder.getLocale()));
        obj.put(DATA, data);
        return obj;
    }

    public static Object ok(String key, Object data) {
        Map<String, Object> obj = new HashMap<>(3);
        obj.put(ERR_NO, 0);
        obj.put(ERR_MSG, messageSource.getMessage("0", null, LocaleContextHolder.getLocale()));
        Map<String, Object> map = new HashMap<>(2);
        map.put(key, data);
        obj.put(DATA, map);
        return obj;
    }

    public static Object okList(List<?> data) {
        Map<String, Object> obj = new HashMap<>(3);
        obj.put(ERR_NO, 0);
        obj.put(ERR_MSG, messageSource.getMessage("0", null, LocaleContextHolder.getLocale()));
        obj.put(DATA, data == null ? new int[0] : data);
        return obj;
    }

    public static Object fail() {
        Map<String, Object> obj = new HashMap<>();
        obj.put(ERR_NO, -1);
        obj.put(ERR_MSG, messageSource.getMessage("-1", null, LocaleContextHolder.getLocale()));
        return obj;
    }

    public static Object fail(int errno, String errMsg) {
        Map<String, Object> obj = new HashMap<>(2);
        obj.put(ERR_NO, errno);
        obj.put(ERR_MSG, errMsg);
        return obj;
    }

    public static Object unLogin() {
        return fail(501, messageSource.getMessage("501", null, LocaleContextHolder.getLocale()));
    }

    public static Object updatedDataFailed() {
        return fail(505, messageSource.getMessage("505", null, LocaleContextHolder.getLocale()));
    }

    public static Object badValue(int errno, String key) {
        return fail(errno, messageSource.getMessage(key, null, LocaleContextHolder.getLocale()));
    }

    public static Object noAuthority(String key) {
        return fail(401, messageSource.getMessage(key, null, LocaleContextHolder.getLocale()));
    }

    public static Object badArgumentValue() {
        return fail(402, messageSource.getMessage("402", null, LocaleContextHolder.getLocale()));
    }

    public static Object serious() {
        return fail(502, messageSource.getMessage("502", null, LocaleContextHolder.getLocale()));
    }

    public static Object unAuth() {
        return fail(506, messageSource.getMessage("506", null, LocaleContextHolder.getLocale()));
    }

    /**
     * 业务错误
     *
     * @param key 国际化key
     * @return ResponseUtil
     */
    public static Object badBussValue(String key) {
        return fail(BUSINESS_ERR_NO, messageSource.getMessage(key, null, LocaleContextHolder.getLocale()));
    }

    public static Object userNoExists() {
        return fail(507, messageSource.getMessage("507", null, LocaleContextHolder.getLocale()));
    }

    private static MessageSource messageSource;

    public static MessageSource getMessageSource() {
        return messageSource;
    }

    @Autowired
    public void setMessageSource(MessageSource messageSource) {
        ResponseUtil.messageSource = messageSource;
    }
}

